package com.yungu.swift.admin.route.finance;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.utils.ExcelUtil;
import com.yungu.swift.assets.driver.model.dto.DriverCashDto;
import com.yungu.swift.assets.driver.model.dto.DriverWithdrawalDto;
import com.yungu.swift.assets.driver.model.param.AdminDriverCashBillPageParam;
import com.yungu.swift.assets.driver.model.param.AdminDriverCashPageParam;
import com.yungu.swift.assets.driver.model.param.AdminDriverCashParam;
import com.yungu.swift.assets.driver.service.DriverCashService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminUuidParam;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yungu.swift.admin.consts.AdminErrorEnum.PARAM_ERROR;
import static com.yungu.swift.assets.driver.model.dto.DriverCashDto.CHECK_STATUS_FAILURE;
import static com.yungu.swift.assets.driver.model.dto.DriverCashDto.UNDER_CHECK_STATUS;

/**
 * @author : cuixiuyin
 * @date : 2019/7/31
 */
@RestController
@RequestMapping("/admin/v1/finance/cash")
@Api(value = "【财务管理】", tags = "司机提现")
public class DriverCashAdmin {

    @Reference
    private DriverCashService driverCashService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "司机提现信息分页查询", httpMethod = "POST")
    public ResponseData<PageVo<DriverCashDto>> queryPage(@RequestBody AdminDriverCashPageParam cashPageParam) {
        return driverCashService.queryPage(cashPageParam);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @ApiOperation(value = "司机提现信息详情", httpMethod = "POST")
    public ResponseData<DriverCashDto> detail(@RequestParam String uuid) {
        return driverCashService.get(uuid);
    }

    @RequestMapping(value = "/review", method = RequestMethod.POST)
    @ApiOperation(value = "司机提现信息审核", httpMethod = "POST")
    public ResponseData<Boolean> review(@RequestBody AdminDriverCashParam driverCashParam) {
        if (ParamUtil.isIllegal(driverCashParam.getUuid(), driverCashParam.getStatus())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        // 提现申请
        if (UNDER_CHECK_STATUS == driverCashParam.getStatus()) {
            return ResponseData.buildSuccessResponse("提现申请", Boolean.TRUE);
        }
        // 审核失败
        if (CHECK_STATUS_FAILURE == driverCashParam.getStatus() && StringUtils.isEmpty(driverCashParam.getReason())) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "审核不通过，失败的原因不能为空");
        }
        return driverCashService.reviewCash(driverCashParam);
    }

    @RequestMapping(value = "/remit", method = RequestMethod.POST)
    @ApiOperation(value = "提现打款", httpMethod = "POST")
    public ResponseData<Boolean> remit(@RequestBody AdminDriverCashParam driverCashParam) {
        if (ParamUtil.isIllegal(driverCashParam.getUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        driverCashParam.setSerialNum(System.currentTimeMillis() + "");
        return driverCashService.remit(driverCashParam);
    }

    @RequestMapping(value = "/billPage", method = RequestMethod.POST)
    @ApiOperation(value = "司机提现账单", httpMethod = "POST")
    public ResponseData<PageVo<DriverWithdrawalDto>> cashBillPage(@RequestBody AdminDriverCashBillPageParam cashBillPageParam) {
        return driverCashService.cashBillPage(cashBillPageParam);
    }

    @RequestMapping(value = "/billExport", method = RequestMethod.POST)
    @ApiOperation(value = "司机提现账单导出", httpMethod = "POST")
    public void cashBillExport(@RequestBody AdminDriverCashBillPageParam cashBillPageParam, HttpServletResponse response) {
        List<DriverWithdrawalDto> list = driverCashService.cashBillExport(cashBillPageParam).getData();
        Map<Object, Object>[] dictionaryMapArray = new Map[8];
        //第三方状态字典
        dictionaryMapArray[6] = new HashMap<>(3);
        dictionaryMapArray[6].put(1, "处理中");
        dictionaryMapArray[6].put(2, "打款成功");
        dictionaryMapArray[6].put(3, "打款失败");
        //提现状态字典
        dictionaryMapArray[7] = new HashMap<>(3);
        dictionaryMapArray[7].put(1, "提现中");
        dictionaryMapArray[7].put(2, "提现成功");
        dictionaryMapArray[7].put(3, "提现失败");
        ExcelUtil.export(response, "提现账单", list, 8,
                new String[]{"提现创建时间", "提现完成时间", "司机信息", "账户信息", "流水号", "提现金额（元）", "第三方平台状态", "提现状态"},
                new String[]{"createOn", "finishTime", "#name#(#mobile#)", "#collectBankName#(#collectAccount#)",
                        "平台流水号:#platformSerialNumber#第三方流水号：#thirdSerialNumber#", "money", "thirdPayStatus", "withdrawalStatus"},
                dictionaryMapArray);
    }


    @RequestMapping(value = "/repay", method = RequestMethod.POST)
    @ApiOperation(value = "司机提现重新打款", httpMethod = "POST")
    public ResponseData<Boolean> repay(@RequestBody AdminUuidParam adminUuidParam) {
        return driverCashService.repay(adminUuidParam);
    }


    @RequestMapping(value = "/cancelPay", method = RequestMethod.POST)
    @ApiOperation(value = "司机提现取消打款", httpMethod = "POST")
    public ResponseData<Boolean> cancelPay(@RequestBody AdminUuidParam adminUuidParam) {
        return driverCashService.cancelPay(adminUuidParam);
    }

}
